package com.example.blog.modules.article.repository;

import com.example.blog.modules.article.entity.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.Map;

@Repository
public interface ArticleRepository extends JpaRepository<Article, Long>, JpaSpecificationExecutor<Article> {
    
    @Modifying
    @Query("UPDATE Article a SET a.viewCount = a.viewCount + 1 WHERE a.id = ?1")
    void incrementViewCount(Long id);
    
    @Modifying
    @Query("UPDATE Article a SET a.likeCount = a.likeCount + 1 WHERE a.id = ?1")
    void incrementLikeCount(Long id);
    
    @Modifying
    @Query("UPDATE Article a SET a.likeCount = a.likeCount - 1 WHERE a.id = ?1 AND a.likeCount > 0")
    void decrementLikeCount(Long id);
    
    @Modifying
    @Query("UPDATE Article a SET a.commentCount = a.commentCount + 1 WHERE a.id = ?1")
    void incrementCommentCount(Long id);
    
    @Modifying
    @Query("UPDATE Article a SET a.commentCount = a.commentCount - 1 WHERE a.id = ?1 AND a.commentCount > 0")
    void decrementCommentCount(Long id);

    /**
     * 统计已发布的文章数量
     */
    long countByIsPublishedTrue();

    /**
     * 统计草稿状态的文章数量
     */
    long countByIsPublishedFalse();

    /**
     * 统计指定时间之后创建的文章数量
     */
    long countByCreatedAtAfter(LocalDateTime dateTime);

    /**
     * 统计指定时间段内创建的文章数量
     */
    long countByCreatedAtBetween(LocalDateTime startDate, LocalDateTime endDate);

    /**
     * 统计指定状态和时间段内的文章数量
     */
    long countByStatusAndCreatedAtBetween(String status, LocalDateTime startDate, LocalDateTime endDate);

    /**
     * 统计指定时间段内的文章浏览量
     */
    @Query("SELECT SUM(a.viewCount) FROM Article a WHERE a.updatedAt BETWEEN :startDate AND :endDate")
    Long sumViewsByUpdateTimeBetween(@Param("startDate") LocalDateTime startDate, 
                                   @Param("endDate") LocalDateTime endDate);

    /**
     * 统计指定时间段内的文章点赞数
     */
    @Query("SELECT SUM(a.likeCount) FROM Article a WHERE a.updatedAt BETWEEN :startDate AND :endDate")
    Long sumLikesByUpdateTimeBetween(@Param("startDate") LocalDateTime startDate, 
                                   @Param("endDate") LocalDateTime endDate);

    /**
     * 获取每日新增文章数
     */
    @Query("SELECT DATE(a.createdAt) as date, COUNT(a) as count " +
           "FROM Article a " +
           "WHERE a.createdAt BETWEEN :startDate AND :endDate " +
           "GROUP BY DATE(a.createdAt)")
    Map<String, Long> findDailyNewArticles(@Param("startDate") LocalDateTime startDate, 
                                         @Param("endDate") LocalDateTime endDate);

    /**
     * 获取每日文章浏览量
     */
    @Query("SELECT DATE(a.updatedAt) as date, SUM(a.viewCount) as count " +
           "FROM Article a " +
           "WHERE a.updatedAt BETWEEN :startDate AND :endDate " +
           "GROUP BY DATE(a.updatedAt)")
    Map<String, Long> findDailyViews(@Param("startDate") LocalDateTime startDate, 
                                   @Param("endDate") LocalDateTime endDate);
} 